<script setup lang="ts">
import * as echarts from 'echarts';
import {onMounted, reactive, ref, watch} from "vue";
import {theme} from "@/components/common/f_theme";
import type {chatStatisticType} from "@/api/data_api";
import {chatStatisticApi} from "@/api/data_api";
import {Message} from "@arco-design/web-vue";

type EChartsOption = echarts.EChartsOption;
let myChart: echarts.ECharts | null = null

const data = reactive<chatStatisticType>({
  dateList: [],
  countList: [],
})

async function getData() {
  const res = await chatStatisticApi()
  if (res.code) {
    Message.error(res.msg)
    return
  }
  Object.assign(data, res.data)
}



function initEcharts() {
  let option: EChartsOption;

  const textColor = getComputedStyle(document.body).getPropertyValue("--color-text-1")
  const lineColor = getComputedStyle(document.body).getPropertyValue("--color-neutral-2")

  let themeColor = [
    '#1c5ae0',
    '#15c5be'
  ]
  if (theme.value === "dark") {
    themeColor = [
      '#1c5ae0',
      '#15c5be'
    ]
  }

  option = {
    color: themeColor,
    title: {
      text: '对话数据',
      textStyle: {
        color: textColor
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: data.dateList
      }
    ],
    yAxis: [
      {
        type: 'value',
        splitLine: {
          lineStyle: {
            color: lineColor
          }
        }
      }
    ],
    series: [
      {
        name: '对话',
        type: 'line',
        // stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series'
        },
        data: data.countList,
        smooth: true,
      },
    ]
  };
  option && myChart?.setOption(option);
}

onMounted(async () => {
  await getData()
  const chartDom = document.querySelector('.user_login_echarts') as HTMLDivElement;
  myChart = echarts.init(chartDom);
  initEcharts()
})

watch(() => theme.value, () => {
  initEcharts()
})


</script>

<template>
  <div class="user_login_echarts"></div>
</template>

<style lang="less">
.user_login_echarts {
  width: 100%;
  height: 230px;
  background-color: var(--color-bg-1);
  border-radius: 5px;
}
</style>